package november.tree.binaryTree;

/**
 * @author huangxin
 * @date 2019/11/20-22:17
 */
public class TreeNode {
    int id;
    String name;
    TreeNode left;
    TreeNode right;

    public TreeNode(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "TreeNode{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    //前序遍历
    public void preOrder() {
        //先输出当前节点
        TreeNode current = this;
        System.out.println(current);

        if (current.left != null) {
            //左边不为空
            current.left.preOrder();
        }
        if (current.right != null) {
            //右边不为空,右边继续前序遍历
            current.right.preOrder();
        }
    }

    //中序遍历
    public void infixOrder() {
        TreeNode current = this;

        if (current.left != null) {
            //左边不为空
            current.left.infixOrder();
        }
        //输出当前节点
        System.out.println(current);

        if (current.right != null) {
            //右边不为空,右边继续前序遍历
            current.right.infixOrder();
        }
    }

    //后序遍历
    public void afterOrder() {
        TreeNode current = this;

        if (current.left != null) {
            //左边不为空
            current.left.afterOrder();
        }
        if (current.right != null) {
            //右边不为空,右边继续前序遍历
            current.right.afterOrder();
        }
        //输出当前节点
        System.out.println(current);
    }


    //前序查找
    public TreeNode preSearch(int id) {
        //比较当前节点
        TreeNode current = this;
        System.out.println("前序查找");
        if (current.id == id) {
            return current;
        }

        TreeNode result;
        if (current.left != null) {
            //左边不为空
            result = current.left.preSearch(id);
            if (result != null) {
                return result;
            }
        }
        if (current.right != null) {
            //右边不为空,右边继续前序查找
            result = current.right.preSearch(id);
            if (result != null) {
                return result;
            }
        }
        return null;
    }


    //中序查找
    public TreeNode infixSearch(int id) {
        //比较当前节点
        TreeNode current = this;


        TreeNode result = null;
        if (current.left != null) {
            //左边不为空
            result = current.left.infixSearch(id);
            if (result != null) {
                return result;
            }
        }
        System.out.println("中序查找");
        if (current.id == id) {
            return current;
        }

        if (current.right != null) {
            //右边不为空,右边继续中序查找
            result = current.right.infixSearch(id);
            if (result != null) {
                return result;
            }
        }
        return null;
    }

    //后序查找
    public TreeNode afterSearch(int id) {
        //比较当前节点
        TreeNode current = this;

        TreeNode result = null;
        if (current.left != null) {
            //左边不为空
            result = current.left.afterSearch(id);
            if (result != null) {
                return result;
            }
        }

        if (current.right != null) {
            //右边不为空,右边继续后序查找
            result = current.right.afterSearch(id);
            if (result != null) {
                return result;
            }
        }
        System.out.println("后序查找");
        if (current.id == id) {
            return current;
        }
        return null;
    }


    //因为二叉树是单向的,所以判断父节点的左右节点,再进行删除
    public void delete(int id) {
        TreeNode current = this;

        //判断左边元素
        if (current.left != null && current.left.id == id) {
            //直接删除子树
            current.left = null;
            return;
        }

        //右边元素
        if (current.right != null && current.right.id == id) {
            //直接删除子树
            current.right = null;
            return;
        }

        //向左递归
        if (current.left != null) {
            current.left.delete(id);
        }


        //向右递归
        if (current.right != null) {
            current.right.delete(id);
        }
    }

}
